<template>
	<view class="container">
		<!-- 订单状态切换 -->
		<scroll-view scroll-x class="status-bar">
			<view class="status-list">
				<view 
					class="status-item" 
					v-for="(item, index) in orderStatus" 
					:key="index"
					:class="{ active: currentStatus === item.type }"
					@click="switchStatus(item.type)"
				>
					<text class="status-name">{{item.name}}</text>
				</view>
			</view>
		</scroll-view>
		
		<!-- 订单列表 -->
		<scroll-view scroll-y class="order-list" @scrolltolower="loadMore">
			<view class="order-item" v-for="(item, index) in orderList" :key="index">
				<!-- 订单头部 -->
				<view class="order-header">
					<text class="order-no">订单号：{{item.orderNo}}</text>
					<text class="order-status">{{item.statusText}}</text>
				</view>
				
				<!-- 商品列表 -->
				<view class="product-list">
					<view class="product-item" v-for="(product, pIndex) in item.products" :key="pIndex">
						<image :src="product.image" mode="aspectFill" class="product-img"></image>
						<view class="product-info">
							<text class="product-name">{{product.name}}</text>
							<text class="product-specs">{{product.specs}}</text>
							<view class="product-bottom">
								<text class="product-price">¥{{product.price}}</text>
								<text class="product-quantity">x{{product.quantity}}</text>
							</view>
						</view>
					</view>
				</view>
				
				<!-- 订单底部 -->
				<view class="order-footer">
					<view class="total-info">
						<text>共{{item.totalQuantity}}件商品</text>
						<text>实付款：</text>
						<text class="total-price">¥{{item.totalPrice}}</text>
					</view>
					<view class="action-buttons">
						<button 
							class="action-btn" 
							v-for="(btn, bIndex) in item.buttons" 
							:key="bIndex"
							@click="handleAction(btn.action, item)"
						>{{btn.text}}</button>
					</view>
				</view>
			</view>
			
			<!-- 加载更多 -->
			<view class="loading-more" v-if="hasMore">
				<text>加载中...</text>
			</view>
			<view class="no-more" v-else>
				<text>没有更多订单了</text>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue'

// 订单状态
const orderStatus = ref([
	{ type: 'all', name: '全部' },
	{ type: 'unpaid', name: '待付款' },
	{ type: 'unshipped', name: '待发货' },
	{ type: 'shipped', name: '待收货' },
	{ type: 'completed', name: '已完成' },
	{ type: 'refund', name: '退款/售后' }
])

// 当前选中的状态
const currentStatus = ref('all')

// 订单列表数据
const orderList = ref([
	{
		orderNo: 'DD202403150001',
		statusText: '待付款',
		totalQuantity: 2,
		totalPrice: '69.8',
		products: [
			{
				name: '重庆老火锅底料',
				specs: '500g',
				price: '39.9',
				quantity: 1,
				image: 'https://img.alicdn.com/imgextra/i4/O1CN01XCiY1B1nx2Xh2Z9tP_!!6000000005155-0-tps-1316-736.jpg'
			},
			{
				name: '特色花椒油',
				specs: '200ml',
				price: '29.9',
				quantity: 1,
				image: 'https://img.alicdn.com/imgextra/i2/O1CN01F0MA5j1zXb0XcYV1F_!!6000000006733-0-tps-1316-736.jpg'
			}
		],
		buttons: [
			{ text: '取消订单', action: 'cancel' },
			{ text: '立即付款', action: 'pay' }
		]
	},
	{
		orderNo: 'DD202403150002',
		statusText: '待发货',
		totalQuantity: 1,
		totalPrice: '49.9',
		products: [
			{
				name: '手工牛肉丸',
				specs: '500g',
				price: '49.9',
				quantity: 1,
				image: 'https://img.alicdn.com/imgextra/i2/O1CN01F0MA5j1zXb0XcYV1F_!!6000000006733-0-tps-1316-736.jpg'
			}
		],
		buttons: [
			{ text: '提醒发货', action: 'remind' }
		]
	}
])

// 是否还有更多数据
const hasMore = ref(true)

// 切换订单状态
const switchStatus = (type) => {
	currentStatus.value = type
	// 这里可以添加获取对应状态订单列表的接口调用
	orderList.value = []
	hasMore.value = true
}

// 加载更多
const loadMore = () => {
	if (!hasMore.value) return
	
	// 这里可以添加加载更多订单的接口调用
	hasMore.value = false
}

// 处理订单操作
const handleAction = (action, order) => {
	switch(action) {
		case 'cancel':
			uni.showModal({
				title: '提示',
				content: '确定要取消该订单吗？',
				success: (res) => {
					if (res.confirm) {
						// 这里可以添加取消订单的接口调用
						uni.showToast({
							title: '订单已取消',
							icon: 'success'
						})
					}
				}
			})
			break
		case 'pay':
			uni.navigateTo({
				url: `/pages/payment/index?orderNo=${order.orderNo}`
			})
			break
		case 'remind':
			uni.showToast({
				title: '已提醒商家发货',
				icon: 'success'
			})
			break
		case 'confirm':
			uni.showModal({
				title: '提示',
				content: '确认已收到商品？',
				success: (res) => {
					if (res.confirm) {
						// 这里可以添加确认收货的接口调用
						uni.showToast({
							title: '确认收货成功',
							icon: 'success'
						})
					}
				}
			})
			break
		case 'refund':
			uni.navigateTo({
				url: `/pages/refund/index?orderNo=${order.orderNo}`
			})
			break
	}
}

// 页面加载
onMounted(() => {
	// 获取订单列表
	// fetchOrderList()
})
</script>

<style>
.container {
	min-height: 100vh;
	background-color: #F5F5F5;
}

/* 状态栏样式 */
.status-bar {
	background-color: #FFFFFF;
	padding: 20rpx 0;
	white-space: nowrap;
}

.status-list {
	display: inline-flex;
	padding: 0 20rpx;
}

.status-item {
	padding: 0 30rpx;
	position: relative;
}

.status-name {
	font-size: 28rpx;
	color: #666;
}

.status-item.active .status-name {
	color: #D02B29;
	font-weight: bold;
}

.status-item.active::after {
	content: '';
	position: absolute;
	bottom: -10rpx;
	left: 50%;
	transform: translateX(-50%);
	width: 40rpx;
	height: 4rpx;
	background-color: #D02B29;
	border-radius: 2rpx;
}

/* 订单列表样式 */
.order-list {
	height: calc(100vh - 100rpx);
}

.order-item {
	background-color: #FFFFFF;
	margin: 20rpx;
	padding: 20rpx;
	border-radius: 10rpx;
}

.order-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding-bottom: 20rpx;
	border-bottom: 1rpx solid #EEEEEE;
}

.order-no {
	font-size: 24rpx;
	color: #999;
}

.order-status {
	font-size: 24rpx;
	color: #D02B29;
}

.product-list {
	padding: 20rpx 0;
}

.product-item {
	display: flex;
	margin-bottom: 20rpx;
}

.product-item:last-child {
	margin-bottom: 0;
}

.product-img {
	width: 160rpx;
	height: 160rpx;
	border-radius: 10rpx;
	margin-right: 20rpx;
}

.product-info {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
}

.product-name {
	font-size: 28rpx;
	color: #333;
}

.product-specs {
	font-size: 24rpx;
	color: #999;
}

.product-bottom {
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.product-price {
	font-size: 28rpx;
	color: #D02B29;
	font-weight: bold;
}

.product-quantity {
	font-size: 24rpx;
	color: #999;
}

.order-footer {
	padding-top: 20rpx;
	border-top: 1rpx solid #EEEEEE;
}

.total-info {
	text-align: right;
	font-size: 24rpx;
	color: #666;
	margin-bottom: 20rpx;
}

.total-price {
	font-size: 32rpx;
	color: #D02B29;
	font-weight: bold;
}

.action-buttons {
	display: flex;
	justify-content: flex-end;
}

.action-btn {
	margin-left: 20rpx;
	padding: 0 30rpx;
	height: 60rpx;
	line-height: 60rpx;
	font-size: 24rpx;
	color: #666;
	background-color: #F5F5F5;
	border-radius: 30rpx;
}

.action-btn:last-child {
	color: #FFFFFF;
	background-color: #D02B29;
}

/* 加载更多样式 */
.loading-more, .no-more {
	text-align: center;
	padding: 20rpx;
	font-size: 24rpx;
	color: #999;
}
</style> 